package com.resou.reader.reader.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.PopupWindow;

import com.resou.reader.R;


/**
 * @author UQIANTU.
 * @date 17/6/6.
 */

public class PopupWindowView implements View.OnClickListener {
    private Context context;

    //PopupWindow 布局View
    private View popupView;

    private PopupWindow popupWindow;

    private int xOff = 8;
    private int yOff = 8;

    /**
     * 构造方法
     *
     * @param context
     */
    public PopupWindowView(Context context) {
        if (context != null) {
            this.context = context;
            popupView = ((Activity) context).getLayoutInflater().inflate(R.layout.view_read_menu, null);
            popupView.findViewById(R.id.RlYjfk).setOnClickListener(this);
            popupView.findViewById(R.id.rlShare).setOnClickListener(this);
            popupView.findViewById(R.id.RlDetail).setOnClickListener(this);
        }
    }

    /**
     * 初始化View
     *
     * @param width    PopupWindow宽度
     * @param height   PopupWindow高度
     * @param dropDown PopupWindow位置间隔
     * @param bgId     PopupWindow背景
     */
    public void initView(int width, int height, int dropDown, int bgId) {
        initPopupWindow(width, height, dropDown, bgId, null);
    }

    /**
     * 初始化PopupWindow
     *
     * @param width    PopupWindow宽度
     * @param height   PopupWindow高度
     * @param dropDown PopupWindow位置间隔
     * @param bgId     PopupWindow背景
     */
    public void initPopupWindow(int width, int height, int dropDown, int bgId, PopupWindow.OnDismissListener onDismissListener) {
        if (popupView != null) {
//            View bg = popupView.findViewById(R.id.popup_window_bg);
//            bg.setBackgroundResource(bgId);

            //创建PopupWindow对象，指定宽度和高度
            popupWindow = new PopupWindow(popupView, width, height);
            //设置动画
            popupWindow.setAnimationStyle(R.style.popup_window_anim);
            //设置背景颜色
            popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
            //设置可以获取焦点
            popupWindow.setFocusable(true);
            //设置可以触摸弹出框以外的区域
            popupWindow.setOutsideTouchable(true);
            //PopupWindow对象关闭回调
            if (onDismissListener != null) {
                popupWindow.setOnDismissListener(onDismissListener);
            }
            //更新popupwindow的状态
            popupWindow.update();
            yOff = dropDown;
        }
    }

    /**
     * 以下拉的方式显示，并且可以设置显示的位置
     * 康复群
     *
     * @param parentView
     */
    public void showPopupWindow(View parentView) {
        try {
            if (popupWindow != null) {
                popupWindow.showAsDropDown(parentView, xOff, yOff);
            }
        } catch (Exception e) {

        }
    }

    /**
     * 以下拉的方式显示，并且可以设置显示的位置
     *
     * @param parentView
     * @param padding
     */
    public void showPopupWindow(View parentView, int padding) {
        try {
            if (popupView != null && popupWindow != null) {
                popupView.setPadding(padding, padding, padding, padding);
                popupWindow.showAsDropDown(parentView, xOff, yOff);
            }
        } catch (Exception e) {

        }
    }

    public void setxOff(int x) {
        xOff = x;
    }

    private OnPopupWindowClickListener listener;

    public interface OnPopupWindowClickListener {
        void onClickPopup(View view);
    }

    public void setOnPopupWindowClickListener(OnPopupWindowClickListener listener) {
        this.listener = listener;
    }


    @Override
    public void onClick(View v) {
        if (listener != null) {
            listener.onClickPopup(v);
            popupWindow.dismiss();
        }
    }
}
